package bysj.service;

import bysj.dao.GraduateProjectSourceDao;
import bysj.domain.GraduateProjectSource;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

public class GraduateProjectSourceService {
    private static GraduateProjectSourceDao graduateProjectSourceDao= GraduateProjectSourceDao.getInstance();
    private static GraduateProjectSourceService graduateProjectSourceService=new GraduateProjectSourceService();
    private GraduateProjectSourceService(){}

    public static GraduateProjectSourceService getInstance(){
        return graduateProjectSourceService;
    }

    public GraduateProjectSource find(Integer id) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        GraduateProjectSource graduateProjectSource=null;
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        try {
            graduateProjectSource=graduateProjectSourceDao.find(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
            e.printStackTrace();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(connection);
        }
        return graduateProjectSource;
    }
}
